/*
* @Author: lungin
* @Date:   2016-03-02 14:49:16
* @Last Modified by:   lungin
* @Last Modified time: 2016-06-03 16:19:17
*/

var pic_index = $("#carousel_index li.on").index();	//当前轮播图索引
var pic_len = $("#carousel_index li").length - 1;	//轮播图片数
var ct;
var topban_st;

//轮播图测试效果，上线可删
//下翻页
function carousel_next(){
	$("#carousel_index li.on").stop().animate({
		opacity: "0"
	},300,function(){
		$(this).removeClass("on");
		if(pic_index == pic_len - 1){
			$(this).next().addClass("on").removeClass("pic_next");
			$("#carousel_index li:first").addClass("pic_next");
			pic_index = pic_len;
		}else if(pic_index == pic_len){
			$("#carousel_index li:first").addClass("on").removeClass("pic_next");
			$("#carousel_index li.on").next().addClass("pic_next");
			pic_index = 0;
		}else{
			$(this).next().addClass("on").removeClass("pic_next");
			$("#carousel_index li.on").next().addClass("pic_next");
			pic_index++;
		}
		$("#carousel_index li.on").stop().animate({
			opacity: "1"
		},300);
		$("#carousel_index .pic_guide i.on").removeClass("on");
		$("#carousel_index .pic_guide i").eq(pic_index).addClass("on");
	});
}
//上翻页
function carousel_prev(){
	$("#carousel_index li.on").stop().animate({
		opacity: "0"
	},300,function(){
		$(this).removeClass("on");
		$(this).next().removeClass("pic_next");
		if(pic_index == 0){
			$("#carousel_index li:last").addClass("on");
			$("#carousel_index li:first").addClass("pic_next");
			pic_index = pic_len;
		}else{
			$(this).prev().addClass("on");
			$(this).addClass("pic_next");
			pic_index--;
		}
		$("#carousel_index li.on").stop().animate({
			opacity: "1"
		},300);
		$("#carousel_index .pic_guide i.on").removeClass("on");
		$("#carousel_index .pic_guide i").eq(pic_index).addClass("on");
	});
}

//页面自适应
function adaption(){
	var pw = $(window).width();
	if(pw <= 1000){
		$("html").removeClass().addClass("w900");
	}else if(pw <= 1100){
		$("html").removeClass().addClass("w1000");
	}else if(pw <= 1200){
		$("html").removeClass().addClass("w1100");
	}else if(pw <= 1300){
		$("html").removeClass().addClass("w1300");
	}else if(pw <= 1400){
		$("html").removeClass().addClass("w1400");
	}else{
		$("html").removeClass();
	}
}

//页面滚动时导航隐藏/出现
function scrollhd(){
	var page_top = $(document).scrollTop();
	var page_height = $(document).height();
	var ph = $(window).height();
	var ph2 = ph*2;
	var guide_arrow = page_height - page_top;
	if(page_top < 70){
		$("#hd_index").css("position","static");
	}else if(page_top <= ph){
		$("#hd_index").css("position","fixed").removeClass("wp-hd-white");
	}else{
		$("#hd_index").css("position","fixed").addClass("wp-hd-white");
	}
	if(guide_arrow < ph2){
		$(".guide_arrow").css("bottom","-30px");
	}else{
		$(".guide_arrow").css("bottom","10px");
	}
}

//页面样式初始化
function l_init(){
	adaption();
	scrollhd();

	//判断消息数字为两位数时，使用椭圆背景。
	/*var msg_val = $("#msg_hd .num").text();
   	if(msg_val >= 10){
   		$("#msg_hd .num").addClass("num_db");
   	}else{
   		$("#msg_hd .num").removeClass("num_db");
   	}*/

   	$("#carousel_index li.on").css("opacity","1");
}

$(document).ready(function(){
	l_init();

   	$(window).scroll(scrollhd);

   	ct = setInterval(carousel_next,5000);

	$(window).resize(adaption);

    //导航hover出二级菜单
    $(".vip-header .menu li").hover(function(){
    	var subnav_ind = $(this).index();
    	$(".subnav_wp").show();
    	$(".subnav_wp .menu").hide();
    	$(".subnav_wp").find(".subnav_"+subnav_ind).show();
    },function(){
    	$(".subnav_wp").hide();
    });
    $(".subnav_wp").hover(function() {
    	$(this).show();
    }, function() {
    	$(this).hide();
    	$(".subnav_wp .menu").hide();;
    });




	//点击轮播图导航
   	$("#carousel_index .pic_guide .dot").mouseenter(function(){
   		clearInterval(ct);
   		pic_index = $(this).index();
   		$("#carousel_index li.on").stop().animate({
   			opacity: "0"
   		},300,function(){
   			$(this).removeClass("on");
	  		$("#carousel_index li").eq(pic_index).addClass("on").stop().animate({
				opacity: "1"
			},300);
   		});
   		$("#carousel_index .pic_guide .dot").removeClass("on");
   		$(this).addClass("on");
   		$("#carousel_index li").removeClass("pic_next");
  		if(pic_index == pic_len){
			$("#carousel_index li:first").addClass("pic_next");
		}else{
			$("#carousel_index li").eq(pic_index+1).addClass("pic_next");
		}
  		ct = setInterval(carousel_next,3000);
   	});

    //轮播图左右按钮翻页
    $("#carousel_index .btn_next").click(function(){
   		clearInterval(ct);
   		carousel_next();
  		ct = setInterval(carousel_next,3000);
    });
    $("#carousel_index .btn_prev").click(function(){
		clearInterval(ct);
		carousel_prev();
  		ct = setInterval(carousel_next,3000);
    });

    //tab页切换
    $("#l_tabs li").click(function(){
    	var tab_index = $(this).index();
    	$("#l_tabs li").removeClass("on");
    	$(this).addClass("on");
    	$("#l_tabcont .mission_area").hide();
    	$("#l_tabcont .mission_area").eq(tab_index).show();
    });

    //抽奖奖品切换
    $("#lottery_change a").click(function(){
		$("#lottery_change a,.mission_lottery .awards_list").toggle();
    });

  //雷鸟背景动画
  	//setInterval(birdAni,60)
	var xlBird = $("#xlBird"),y = 0;
	function birdAni(){
		if(Math.abs(y) > 7200){
			y = 0;
		}
		xlBird.css({"background-position":"0px "+ y +"px"});
		y += -300;
	}
	//闪电背景动画
    setInterval(slothAni,60);
	var sloth = $("#sloth"),y = 0;
	function slothAni(){
		if(Math.abs(y) > 4200){
			y = 0;
		}
	    sloth.css({"background-position":"0px "+ y +"px"});
	    y += -300;
	}

	//top banner
	topban_st = setTimeout(function(){
		$(".ban_top_wp").animate({
			height:"60px"
		},500,function(){
			$(this).addClass("ban_top_packup").css("height","auto");
		});
	},5000);

});

// parallax 视差滚动
function Parallax(opt) {
	this.$ele = $(opt.ele);
	this.target = opt.target;
	this.distance = opt.distance || 9;	//可滚动距离百分比 9代码9%
}

Parallax.prototype.scroll = function() {
	var that = this;

	var _scrolltop = $(document).scrollTop();

	for(var i = 0, len = that.$ele.length; i < len; i += 1) {
		var $cur = $(that.$ele[i]),
			_winheight = $(window).height(),
			_pagetop = $cur.offset().top,
			_diff = _winheight - (_pagetop - _scrolltop),
			_pageheight = $cur.height();

		if(_diff >= 0 && _diff < _pageheight) {
			var val = _diff / _pageheight * this.distance;
			$cur.find(that.target).css({
				'-webkit-transform': 'translateY(' + -val + '%)',
				'transform': 'translateY(' + -val + '%)'
			});
		}
	}

}

//翻页滚动
function Slider(opt) {
	this.$ele = $(opt.ele);
	this.liminal = opt.liminal || 160; //阈值
	this.time = opt.time || 100;
	this.isScroll = false;
	this.exScrolltop = 0;
}
Slider.prototype.scroll = function() {
	var that = this;

	var _scrolltop = $(document).scrollTop(),
		_pagetop = $(that.$ele[0]).offset().top,
		_pageheight = $(that.$ele[0]).height(),
		_winheight = $(window).height(),
		_diff = _winheight - (_pagetop - _scrolltop);


	var index = Math.floor(_diff / _pageheight),
		diff = _diff % _pageheight,
		forward = false,
		liminal = that.liminal;

	if(_scrolltop > that.exScrolltop) {
		forward = true;
		that.exScrolltop = _scrolltop;
	}
	else {
		forward = false;
		that.exScrolltop = _scrolltop;
	}

	if(index == 0) {
		liminal = _pageheight / 2;
	}
	else {
		liminal = that.liminal;
	}
	if(index >= 0 && index < that.$ele.length && !that.isScroll && forward && diff > liminal && diff < _pageheight / 4 * 3) {
		that.isScroll = true;
		$('html,body').animate(
			{scrollTop: $(that.$ele[index]).offset().top + 'px'},
			{
				duration: that.time,
				complete: function() { that.isScroll = false; }
			}
		);

	}
}


var parallax = new Parallax({
	ele: '.scnitem',
	target: '.img_scn img',
	distance: 9
});
// var slider = new Slider({
// 	ele: '.scnitem',
// 	liminal: 160,
// 	time: 800

// })

$(window).scroll(function() {
	parallax.scroll();
});
